package com.fruit.controller.wap.invite;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.sms.SmsCodeValidate;
import com.fruit.pojo.user.UserInfo;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtil;

/**
 * 邀请好友controller
 * 
 * @author Administrator
 * @version 2017.12.01 11:40
 */
@Controller
@RequestMapping(value = "/wap/")
public class InviteController extends BaseController {

	@Autowired
	private UserManager userService;

	@Autowired
	private SmsCodeManager smsCodeService;
	
	 @Value("${oss.url}")
	 private String ossUrl;
	 
	 @Value("${iOS.url}")
	 private String iOSUrl;

	/**
	 * 邀请注册
	 * @param ul UserLogin
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "invite", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> register(UserLogin ul, HttpServletRequest request) {
		this.logBefore(logger, "邀请注册");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String userId = request.getParameter("userId");
		String type = request.getParameter("type");
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if (StringUtils.isEmpty(type)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if (StringUtils.isEmpty(userId)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			ul.setUserId(Integer.parseInt(userId));
			resMap = userService.saveInviteUser(ul, type);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	/**
	 * 发送验证码
	 * @param ul UserLogin
	 * @param type type
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/send", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> send(UserLogin ul, String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(type)) {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
				return resMap;
			}
			UserLogin login = userService.getUserLoginByPhone(ul);
			if (StringUtils.equals(type, "login") || StringUtils.equals(type, "password")) {
				if (login == null) {
					resMap.put("result", this.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
					return resMap;
				}
			}
			if (StringUtils.equals(type, "register") || StringUtils.equals(type, "invite")) {
				if (login != null) {
					resMap.put("result", this.initResult(ResultInfo.ERROR, "is_exists", "该手机号码已注册!"));
					return resMap;
				}
			}
			SmsCodeValidate sms = new SmsCodeValidate();
			sms.setPhone(ul.getPhone());
			sms.setTypes(type);
			resMap = smsCodeService.sendSmsCode(sms);

		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "发送失败"));
		}
		return resMap;
	}

	/**
	 * 解析二维码
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "getScanCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getScanCode(HttpServletRequest request) {
		this.logBefore(logger, "解析二维码");
		String code = request.getParameter("code");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (code != null && !"".equals(code)) {
				String userId = code.substring(4, code.length() - 3);
				UserInfo userInfo = userService.getUser(Integer.parseInt(userId));
				if (userInfo != null) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
					resMap.put("userInfo", userInfo);
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "未找到用户!"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	/**
	 * 下载地址
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "getDownloadUrl", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getDownloadUrl(HttpServletRequest request) {
		this.logBefore(logger, "下载地址");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if(!"".equals(ossUrl) && ossUrl != null && iOSUrl != null){
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "获取成功!"));
				String Android=ossUrl+"/fruit/apk/Android/fruit.apk";
				resMap.put("Android", Android);
				resMap.put("iOS", iOSUrl);
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "获取失败!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	

}
